package com.woniumall.service.impl;

import com.woniumall.dao.UserDao;
import com.woniumall.pojo.Massage;
import com.woniumall.pojo.User;
import com.woniumall.service.UserService;
import com.woniumall.util.MailUtils;
import com.woniumall.util.sqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import java.util.UUID;

/**
 * @auther linke
 * @create 2021-10-29 16:52
 */

public class UserServiceImpl implements UserService {
    private  SqlSession sqlSession = null;
    private  UserDao userDao = null;
    @Override
    public Massage checkUsername(String username)  {
        sqlSession = sqlSessionUtil.sqlSession();
        userDao = sqlSession.getMapper(UserDao.class);

        User user = userDao.findByUsername(username);
        if(user == null){
            //用户不存在，可以创建
            Massage msg = new Massage(false,"用户名可用");
            sqlSession.close();//释放资源
            return msg;
        }else {
            //用户已存在，返回提示信息
            Massage msg = new Massage(false,"用户名已存在");
            sqlSession.close();//释放资源
            return msg;
        }

    }
    //登录检查用户名和激活状态
    @Override
    public Massage checkLogin(String username) {
        sqlSession = sqlSessionUtil.sqlSession();
        userDao = sqlSession.getMapper(UserDao.class);

        User user = userDao.findByUsername(username);
        if(user != null ){
            if(user.getStatus().equals("1")){
                //用户存在,已激活
                Massage msg = new Massage(true,"用户名正确");
                sqlSession.close();//释放资源
                return msg;
            }else {
                //用户存在,判断是否激活
                Massage msg = new Massage(false,"用户未激活，请先激活");
                sqlSession.close();//释放资源
                return msg;
            }
        } else {
            //用户不存在，返回提示信息
            Massage msg = new Massage(false,"用户名不存在");
            sqlSession.close();//释放资源
            return msg;
        }
    }

    //增加用户
    @Override
    public void register(User user) {
        sqlSession = sqlSessionUtil.sqlSession();
        userDao = sqlSession.getMapper(UserDao.class);

        //生成UUID
        String uuid = UUID.randomUUID().toString().replace("-", "");
        user.setCode(uuid);
        userDao.addUser(user);
        sqlSession.commit();

        String code = "<a href='localhost:8080/book/user/active?code="+uuid+"'>点击链接激活</a>";
        MailUtils.sendMail(user.getEmail(),code,"注册激活");
        sqlSession.close();//释放资源
    }

    @Override
    public void active(String code) {
        sqlSession = sqlSessionUtil.sqlSession();
        userDao = sqlSession.getMapper(UserDao.class);

        userDao.updateStatus(code);

        sqlSession.commit();
        sqlSession.close();//释放资源
    }

    @Override
    public User login(String username, String password) {
        sqlSession = sqlSessionUtil.sqlSession();
        userDao = sqlSession.getMapper(UserDao.class);

      return userDao.findByUsernameAndPassword(username, password);

    }

    @Override
    public int updatePassword(int id, String password) {
        sqlSession = sqlSessionUtil.sqlSession();
        userDao = sqlSession.getMapper(UserDao.class);

        int row = userDao.updatePassword(id,password);
        sqlSession.commit();
        sqlSession.close();//释放资源
        return row;
    }
    //根据用户名，和图片地址修改user头像
    @Override
    public int updateImage(int id, String saveFilename) {
        sqlSession = sqlSessionUtil.sqlSession();
        userDao = sqlSession.getMapper(UserDao.class);

        int row = userDao.updateImage(id,saveFilename);
        sqlSession.commit();
        sqlSession.close();//释放资源
        return row;
    }
}
